home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / histogram / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-01  |  3.8 KB  |  196 lines

  1. /* histogram/init.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <stdlib.h>
  22. #include <gsl/gsl_errno.h>
  23. #include <gsl/gsl_histogram.h>
  24.  
  25. gsl_histogram *
  26. gsl_histogram_alloc (size_t n)
  27. {
  28.   gsl_histogram *h;
  29.  
  30.   if (n == 0)
  31.     {
  32.       GSL_ERROR_VAL ("histogram length n must be positive integer",
  33.             GSL_EDOM, 0);
  34.     }
  35.  
  36.   h = (gsl_histogram *) malloc (sizeof (gsl_histogram));
  37.  
  38.   if (h == 0)
  39.     {
  40.       GSL_ERROR_VAL ("failed to allocate space for histogram struct",
  41.             GSL_ENOMEM, 0);
  42.     }
  43.  
  44.   h->range = (double *) malloc ((n + 1) * sizeof (double));
  45.  
  46.   if (h->range == 0)
  47.     {
  48.       free (h);        /* exception in constructor, avoid memory leak */
  49.  
  50.       GSL_ERROR_VAL ("failed to allocate space for histogram ranges",
  51.             GSL_ENOMEM, 0);
  52.     }
  53.  
  54.   h->bin = (double *) malloc (n * sizeof (double));
  55.  
  56.   if (h->bin == 0)
  57.     {
  58.       free (h->range);
  59.       free (h);        /* exception in constructor, avoid memory leak */
  60.  
  61.       GSL_ERROR_VAL ("failed to allocate space for histogram bins",
  62.             GSL_ENOMEM, 0);
  63.     }
  64.  
  65.   h->n = n;
  66.  
  67.   return h;
  68. }
  69.  
  70. gsl_histogram *
  71. gsl_histogram_calloc_uniform (const size_t n, const double xmin,
  72.                   const double xmax)
  73. {
  74.   gsl_histogram *h;
  75.  
  76.   if (xmin >= xmax)
  77.     {
  78.       GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
  79.     }
  80.  
  81.   h = gsl_histogram_calloc (n);
  82.  
  83.   if (h == 0)
  84.     {
  85.       return h;
  86.     }
  87.  
  88.   {
  89.     size_t i;
  90.  
  91.     for (i = 0; i < n + 1; i++)
  92.       {
  93.     h->range[i] = xmin + ((double) i / (double) n) * (xmax - xmin);
  94.       }
  95.   }
  96.  
  97.   return h;
  98. }
  99.  
  100. gsl_histogram *
  101. gsl_histogram_calloc (size_t n)
  102. {
  103.   gsl_histogram * h = gsl_histogram_alloc (n);
  104.  
  105.   if (h == 0)
  106.     {
  107.       return h;
  108.     }
  109.  
  110.   {
  111.     size_t i;
  112.  
  113.     for (i = 0; i < n + 1; i++)
  114.       {
  115.     h->range[i] = i;
  116.       }
  117.  
  118.     for (i = 0; i < n; i++)
  119.       {
  120.     h->bin[i] = 0;
  121.       }
  122.   }
  123.  
  124.   h->n = n;
  125.  
  126.   return h;
  127. }
  128.  
  129.  
  130. void
  131. gsl_histogram_free (gsl_histogram * h)
  132. {
  133.   free (h->range);
  134.   free (h->bin);
  135.   free (h);
  136. }
  137.  
  138. /* These initialization functions suggested by Achim Gaedke */
  139.  
  140. int 
  141. gsl_histogram_set_ranges_uniform (gsl_histogram * h, double xmin, double xmax)
  142. {
  143.   size_t i;
  144.   const size_t n = h->n;
  145.  
  146.   if (xmin >= xmax)
  147.     {
  148.       GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
  149.     }
  150.  
  151.   /* initialize ranges */
  152.  
  153.   for (i = 0; i <= n; i++)
  154.     {
  155.       h->range[i] = xmin + ((double) i / (double) n) * (xmax - xmin);
  156.     }
  157.  
  158.   /* clear contents */
  159.  
  160.   for (i = 0; i < n; i++)
  161.     {
  162.       h->bin[i] = 0;
  163.     }
  164.  
  165.   return GSL_SUCCESS;
  166. }
  167.  
  168. int 
  169. gsl_histogram_set_ranges (gsl_histogram * h, const double range[], size_t size)
  170. {
  171.   size_t i;
  172.   const size_t n = h->n;
  173.  
  174.   if (size != (n+1))
  175.     {
  176.       GSL_ERROR_VAL ("size of range must match size of histogram", 
  177.                      GSL_EINVAL, 0);
  178.     }
  179.  
  180.   /* initialize ranges */
  181.  
  182.   for (i = 0; i <= n; i++)
  183.     {
  184.       h->range[i] = range[i];
  185.     }
  186.  
  187.   /* clear contents */
  188.  
  189.   for (i = 0; i < n; i++)
  190.     {
  191.       h->bin[i] = 0;
  192.     }
  193.  
  194.   return GSL_SUCCESS;
  195. }
  196.